<?php

function message($data, $message = 'success', $code = 200)
{
    return [
        'data' => $data,
        'message' => $message,
        'code' => $code
    ];
}

function json($data)
{
    // 允许所有来源的跨域请求
    header('Access-Control-Allow-Origin: *');
    // 允许特定的HTTP方法
    header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD');
    // 允许特定的请求头
    header('Access-Control-Allow-Headers: Content-Type, Authorization, *');
    // 允许携带凭证（如Cookies）
    header('Access-Control-Allow-Credentials: true');

    header('Content-Type: application/json');
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit();
}

function get_param($name, $default = null)
{
    return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
}

function savelog(string $filename, string $message, $isEcho = true): void
{
    $message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
    if ($isEcho) {
        echo $message, PHP_EOL;
    }
    file_put_contents($filename, $message, FILE_APPEND);
}
/**
 * 解析列的数据
 * @param array $results
 */
function parseColumns($results)
{
    $columns = [[], [], [], [], [], [], []];
    foreach ($results as $result) {
        $arr = explode(' ', $result);
        foreach ($arr as $k => $v) {
            if (isset($columns[$k][$v])) {
                $columns[$k][$v]++;
            } else {
                $columns[$k][$v] = 1;
            }
        }
    }

    for ($i = 0; $i < count($columns); $i++) {
        array_multisort($columns[$i], array_values($columns[$i]));
    }
    return $columns;
}

function filteColumns($columns, $min = 2)
{
    for ($i = 0; $i < count($columns); $i++) {
        $columns[$i] = array_filter($columns[$i], function ($v) use ($min) {
            return $v >= $min;
        });
    }
    return $columns;
}

/**
 * 生成订单号码
 * 算法：
 * @param array $columns
 * @param int $n
 * @param array $skipResultArr
 * @return array
 */
function generateOrders($columns, $n = 10, $skipResultArr = [])
{
    $orders = [];
    while ($n > 0) {
        $order = [];
        foreach ($columns as $column) {
            $order[] = $column[array_rand($column)];
        }
        if (count(array_intersect($order, $skipResultArr)) >= 2) {
            continue;
        }
        $orders[] = implode(' ', $order);
        $n--;
    }
    return $orders;
}

/**
 * 计算订单中奖情况
 * @param string $result
 * @param array $orders
 * @return array
 */
function calcOrders($result, $orders, $splitIndex, $prizeList)
{
    $data = [];
    foreach ($orders as $order) {
        $prizeData = ['value' => 0, 'text' => '未中奖', 'front' => 0, 'back' => 0, 'money' => 0, 'moneyText' => ''];
        $resultArr = array_chunk(explode(' ', $result), $splitIndex);
        $orderArr = array_chunk(explode(' ', $order), $splitIndex);
        $winFrontNums = array_intersect($resultArr[0], $orderArr[0]);
        $winBackNums = array_intersect($resultArr[1], $orderArr[1]);
        $prizeData['front'] = count($winFrontNums);
        $prizeData['back'] = count($winBackNums);
        foreach ($prizeList as $item) {
            if ($prizeData['front'] == $item['front'] && $prizeData['back'] == $item['back']) {
                $prizeData = $item;
                break;
            }
        }
        $data[] = ['order' => $order, 'prizeData' => $prizeData, 'winNums' => [$winFrontNums, $winBackNums]];
    }
    return $data;
}
